﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace pb_ws_client
{
    public partial class BranchInfo : Form
    {
        private pb_ws_client.WebServiceReference.branch_info_r _bi;
        private WebServiceReference.AppServiceSoapClient _wsr =
            new WebServiceReference.AppServiceSoapClient();
        private main parent = new main();

        public BranchInfo(WebServiceReference.AppServiceSoapClient wsr, main _form)
        {
            InitializeComponent();
            _wsr = wsr;
            parent = _form;
        }

        public BranchInfo(WebServiceReference.AppServiceSoapClient wsr, main _form, 
            pb_ws_client.WebServiceReference.branch_info_r bi)
        {
            InitializeComponent();
            _wsr = wsr;
            parent = _form;
            _bi = new pb_ws_client.WebServiceReference.branch_info_r();
            _bi = bi;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (Year_textBox.Text == "" ||
                Brname_textBox.Text == "")
            {
                MessageBox.Show("您还有未完成的表单", "警告", MessageBoxButtons.OK);
                return;
            }
            if (!IsRegEx("^(-?[0-9]*[.]*[0-9]{0,3})$", Year_textBox.Text))
            {
                MessageBox.Show("请输入正确的年份", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            pb_ws_client.WebServiceReference.branch_info_r newbi =
                new pb_ws_client.WebServiceReference.branch_info_r();
            newbi.YEAR = Year_textBox.Text;
            newbi.BR_NAME = Brname_textBox.Text;
            if (!newbi.Equals(_bi))
            {
                DialogResult result = new DialogResult();
                result = MessageBox.Show("您确定提交表单吗？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (result == DialogResult.No)
                    return;

                if (_bi.ID == null)
                {
                    try
                    {
                        _wsr.AddBranch(LogicLayer.SID, newbi);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("数据库异常！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    parent.refreshData();
                    this.Close();
                }
                else
                {
                    newbi.ID = _bi.ID;
                    try
                    {
                        _wsr.UpdateBranch(LogicLayer.SID, newbi);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("数据库异常！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    parent.refreshData();
                    this.Close();

                }
            }
            else
                this.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BranchInfo_Load(object sender, EventArgs e)
        {
            if (_bi == null)
            {
                _bi = new pb_ws_client.WebServiceReference.branch_info_r();
            }
            else
            {
                Year_textBox.Text = _bi.YEAR;
                Brname_textBox.Text = _bi.BR_NAME;
            }
        }

        private static bool IsRegEx(string regExValue, string itemValue)
        {

            try
            {
                Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
                if (regex.IsMatch(itemValue)) return true;

                else return false;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
            }
        }


    }
}
